;;--------------------------------------------------
;;--------------------------------------------------
;;
;;                PRACTICA 2 IA: SBC
;;
;;      Alumnos:     Oriol Ganduxï¿œ Pregona
;;			   Sergi Capï¿œ del Nido
;;           
;;      Cuatrimestre: 2011-12 Q2
;;
;;--------------------------------------------------
;;--------------------------------------------------



;;--------------------------------------------------
;;                    ONTOLOGIA
;;--------------------------------------------------

; Sun May 27 16:01:38 CEST 2012
; 
;+ (version "3.4.8")
;+ (build "Build 629")


(defclass %3ACLIPS_TOP_LEVEL_SLOT_CLASS "Fake class to save top-level slot information"
	(is-a USER)
	(role abstract)
	(multislot genere_preferit
		(type INSTANCE)
;+		(allowed-classes Genere)
		(create-accessor read-write))
	(single-slot pais_lec
		(type STRING)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(single-slot dni
		(type STRING)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(single-slot digital
		(type SYMBOL)
		(allowed-values FALSE TRUE)
		(default FALSE)
;+		(cardinality 1 1)
		(create-accessor read-write))
	(single-slot puntuacio_personal
		(type INTEGER)
		(range 0 10)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(single-slot pais_esc
		(type STRING)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(single-slot internacional
		(type SYMBOL)
		(allowed-values FALSE TRUE)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(single-slot nom
		(type STRING)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(single-slot KB_109530_Class28
		(type STRING)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(single-slot format
		(type SYMBOL)
		(allowed-values de_butxaca gran)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(multislot ha_escrit
		(type INSTANCE)
;+		(allowed-classes Llibre)
		(create-accessor read-write))
	(multislot ha_llegit
		(type INSTANCE)
;+		(allowed-classes)
		(create-accessor read-write))
	(single-slot tipus
		(type SYMBOL)
		(allowed-values divertit sexe violent predictible convencional masc0_fem10 duracio)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(single-slot sexe
		(type SYMBOL)
		(allowed-values masc fem)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(single-slot ref_llibre
		(type INSTANCE)
;+		(allowed-classes Llibre)
;+		(cardinality 1 1)
		(create-accessor read-write))
	(single-slot llegit_per
		(type INSTANCE)
;+		(allowed-classes)
;+		(cardinality 1 1)
		(create-accessor read-write))
	(single-slot puntuacio
		(type FLOAT)
;+		(value 0.0)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(single-slot preu
		(type FLOAT)
;+		(cardinality 1 1)
		(create-accessor read-write))
	(single-slot nom_genere
		(type STRING)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(multislot genere_esc_preferit
		(type INSTANCE)
;+		(allowed-classes Genere)
		(create-accessor read-write))
	(multislot genere
		(type INSTANCE)
;+		(allowed-classes Genere)
		(cardinality 1 ?VARIABLE)
		(create-accessor read-write))
	(multislot genere_lec_preferit
		(type INSTANCE)
;+		(allowed-classes Genere)
		(create-accessor read-write))
	(single-slot pagines
		(type INTEGER)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(single-slot punts
		(type FLOAT)
		(default 1.0)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(single-slot best-seller
		(type SYMBOL)
		(allowed-values FALSE TRUE)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(single-slot titol
		(type STRING)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(single-slot edat
		(type STRING)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(multislot te_caracteristica
		(type INSTANCE)
;+		(allowed-classes Caracteristica)
		(create-accessor read-write))
	(multislot autor_preferit
		(type INSTANCE)
;+		(allowed-classes Autor)
		(create-accessor read-write))
	(single-slot escrit_per
		(type INSTANCE)
;+		(allowed-classes Autor)
;+		(cardinality 1 1)
		(create-accessor read-write))
	(single-slot de_moda
		(type SYMBOL)
		(allowed-values FALSE TRUE)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(single-slot llibres_Class20000
		(type STRING)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(single-slot nota_tipus
		(type INTEGER)
		(range 0 10)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(single-slot critica
		(type INTEGER)
		(range 0 10)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(multislot genere_semblant
		(type INSTANCE)
;+		(allowed-classes Genere)
		(create-accessor read-write)))

(defclass Autor
	(is-a USER)
	(role concrete)
	(single-slot pais_esc
		(type STRING)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(multislot ha_escrit
		(type INSTANCE)
;+		(allowed-classes Llibre)
		(create-accessor read-write))
	(single-slot internacional
		(type SYMBOL)
		(allowed-values FALSE TRUE)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(single-slot nom
		(type STRING)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(multislot genere_esc_preferit
		(type INSTANCE)
;+		(allowed-classes Genere)
		(create-accessor read-write)))

(defclass Llibre
	(is-a USER)
	(role concrete)
	(multislot genere
		(type INSTANCE)
;+		(allowed-classes Genere)
		(cardinality 1 ?VARIABLE)
		(create-accessor read-write))
	(single-slot best-seller
		(type SYMBOL)
		(allowed-values FALSE TRUE)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(single-slot escrit_per
		(type INSTANCE)
;+		(allowed-classes Autor)
;+		(cardinality 1 1)
		(create-accessor read-write))
	(single-slot digital
		(type SYMBOL)
		(allowed-values FALSE TRUE)
		(default FALSE)
;+		(cardinality 1 1)
		(create-accessor read-write))
	(single-slot de_moda
		(type SYMBOL)
		(allowed-values FALSE TRUE)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(single-slot preu
		(type FLOAT)
;+		(cardinality 1 1)
		(create-accessor read-write))
	(single-slot titol
		(type STRING)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(single-slot critica
		(type INTEGER)
		(range 0 10)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(single-slot punts
		(type FLOAT)
		(default 1.0)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(multislot te_caracteristica
		(type INSTANCE)
;+		(allowed-classes Caracteristica)
		(create-accessor read-write))
	(single-slot format
		(type SYMBOL)
		(allowed-values de_butxaca gran)
;+		(cardinality 0 1)
		(create-accessor read-write)))

(defclass Genere
	(is-a USER)
	(role concrete)
	(single-slot nom_genere
		(type STRING)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(multislot genere_semblant
		(type INSTANCE)
;+		(allowed-classes Genere)
		(create-accessor read-write)))

(defclass Caracteristica
	(is-a USER)
	(role concrete)
	(single-slot tipus
		(type SYMBOL)
		(allowed-values divertit sexe violent predictible convencional masc0_fem10 duracio)
;+		(cardinality 0 1)
		(create-accessor read-write))
	(single-slot nota_tipus
		(type INTEGER)
		(range 0 10)
;+		(cardinality 0 1)
		(create-accessor read-write)))


;;--------------------------------------------------------------------------------------------------
;;                    INSTANCIES
;;--------------------------------------------------------------------------------------------------

(definstances instancies

; Sun May 27 17:59:49 CEST 2012
; 
;+ (version "3.4.8")
;+ (build "Build 629")

([KB_109530_Class37] of  Genere

	(genere_semblant
		[KB_109530_Class38]
		[llibres_Class5]
		[llibres_Class10]
		[llibres_Class4]
		[llibres_Class6])
	(nom_genere "Ciencia Ficcio"))

([KB_109530_Class38] of  Genere

	(genere_semblant [KB_109530_Class37])
	(nom_genere "Futurista"))

([KB_109530_Class39] of  Genere

	(genere_semblant
		[llibres_Class7]
		[KB_109530_Class42]
		[llibres_Class6])
	(nom_genere "Terror"))

([KB_109530_Class40] of  Genere

	(nom_genere "RomanÃ§"))

([KB_109530_Class41] of  Genere

	(nom_genere "Mitjaval"))

([KB_109530_Class42] of  Genere

	(genere_semblant
		[KB_109530_Class39]
		[llibres_Class7]
		[llibres_Class9]
		[llibres_Class6])
	(nom_genere "Postapocaliptic"))

([KB_109530_Class44] of  Llibre

	(best-seller TRUE)
	(critica 8)
	(escrit_per [KB_109530_Class45])
	(format gran)
	(genere
		[KB_109530_Class41]
		[KB_109530_Class64]
		[llibres_Class7]
		[llibres_Class2]
		[recomanacio_Class20000])
	(preu 15.5)
	(titol "The Lord of the Rings I"))

([KB_109530_Class45] of  Autor

	(genere_esc_preferit
		[llibres_Class2]
		[KB_109530_Class41])
	(ha_escrit
		[KB_109530_Class44]
		[llibres_Class18])
	(internacional TRUE)
	(nom "J.R.R Tolkien")
	(pais_esc "UK"))

([KB_109530_Class49] of  Autor

	(genere_esc_preferit
		[llibres_Class7]
		[llibres_Class13]
		[llibres_Class16])
	(internacional TRUE)
	(nom "Gabriel GarcÃ­a MÃ¡rquez")
	(pais_esc "COL"))

([KB_109530_Class50] of  Autor

	(genere_esc_preferit
		[llibres_Class7]
		[llibres_Class16])
	(ha_escrit
		[llibres_Class11]
		[llibres_Class12])
	(internacional TRUE)
	(nom "Alejandro Dumas")
	(pais_esc "FRA"))

([KB_109530_Class51] of  Autor

	(genere_esc_preferit [llibres_Class13])
	(ha_escrit [llibres_Class28])
	(internacional TRUE)
	(nom "Victor Hugo")
	(pais_esc "FRA"))

([KB_109530_Class52] of  Llibre

	(critica 7)
	(escrit_per [KB_109530_Class51])
	(format gran)
	(genere
		[llibres_Class13]
		[llibres_Class16]
		[llibres_Class7]
		[recomanacio_Class20000])
	(preu 10.95)
	(titol "Noventa y tres"))

([KB_109530_Class57] of  Autor

	(genere_esc_preferit
		[llibres_Class7]
		[KB_109530_Class39])
	(ha_escrit
		[llibres_Class23]
		[llibres_Class24])
	(internacional TRUE)
	(nom "Stephen King")
	(pais_esc "USA"))

([KB_109530_Class58] of  Autor

	(genere_esc_preferit
		[KB_109530_Class64]
		[KB_109530_Class40]
		[llibres_Class9])
	(ha_escrit
		[llibres_Class25]
		[llibres_Class27]
		[llibres_Class10000])
	(internacional TRUE)
	(nom "Stephenie Meyer")
	(pais_esc "USA"))

([KB_109530_Class59] of  Autor

	(genere_esc_preferit [llibres_Class1])
	(ha_escrit
		[llibres_Class17]
		[llibres_Class0])
	(nom "Eduardo Mendoza")
	(pais_esc "ESP"))

([KB_109530_Class60] of  Autor

	(genere_esc_preferit
		[llibres_Class7]
		[llibres_Class13])
	(internacional TRUE)
	(nom "Emily Bronte")
	(pais_esc "UK"))

([KB_109530_Class61] of  Autor

	(genere_esc_preferit
		[llibres_Class7]
		[llibres_Class13]
		[llibres_Class16])
	(ha_escrit [KB_109530_Class62])
	(internacional TRUE)
	(nom "Jane Austen")
	(pais_esc "UK"))

([KB_109530_Class62] of  Llibre

	(best-seller TRUE)
	(critica 6)
	(escrit_per [KB_109530_Class61])
	(format de_butxaca)
	(genere
		[llibres_Class7]
		[llibres_Class13]
		[llibres_Class16]
		[recomanacio_Class20000])
	(preu 9.5)
	(titol "Orgullo y prejuicio"))

([KB_109530_Class63] of  Genere

	(nom_genere "Infantil"))

([KB_109530_Class64] of  Genere

	(nom_genere "Juvenil"))

([llibres_Class0] of  Llibre

	(critica 7)
	(escrit_per [KB_109530_Class59])
	(format de_butxaca)
	(genere
		[llibres_Class10]
		[llibres_Class1]
		[KB_109530_Class64]
		[llibres_Class7]
		[recomanacio_Class20001])
	(preu 10.5)
	(titol "Sin noticias de Gurb"))

([llibres_Class1] of  Genere

	(nom_genere "Humor"))

([llibres_Class10] of  Genere

	(genere_semblant
		[KB_109530_Class37]
		[llibres_Class5]
		[KB_109530_Class38]
		[llibres_Class4]
		[KB_109530_Class39]
		[llibres_Class9]
		[llibres_Class6])
	(nom_genere "Extraterrestres"))

([llibres_Class10000] of  Llibre

	(best-seller TRUE)
	(critica 7)
	(de_moda TRUE)
	(escrit_per [KB_109530_Class58])
	(format gran)
	(genere
		[KB_109530_Class64]
		[KB_109530_Class40]
		[llibres_Class9]
		[llibres_Class7]
		[recomanacio_Class20001])
	(preu 22.5)
	(titol "Lluna nova"))

([llibres_Class10001] of  Autor

	(genere_esc_preferit
		[llibres_Class2]
		[KB_109530_Class64])
	(ha_escrit [llibres_Class10002])
	(internacional TRUE)
	(nom "J.K.Rowling")
	(pais_esc "UK"))

([llibres_Class10002] of  Llibre

	(best-seller TRUE)
	(critica 8)
	(de_moda TRUE)
	(escrit_per [llibres_Class10001])
	(genere
		[llibres_Class2]
		[KB_109530_Class64]
		[KB_109530_Class63]
		[llibres_Class7]
		[recomanacio_Class20001])
	(preu 22.5)
	(titol "Harry Potter y la Piedra Filosofal"))

([llibres_Class11] of  Llibre

	(critica 6)
	(escrit_per [KB_109530_Class50])
	(format gran)
	(genere
		[llibres_Class7]
		[KB_109530_Class40]
		[recomanacio_Class20000])
	(preu 17.95)
	(titol "El caso de la viuda Lafarge"))

([llibres_Class12] of  Llibre

	(best-seller TRUE)
	(critica 8)
	(escrit_per [KB_109530_Class50])
	(format gran)
	(genere
		[llibres_Class7]
		[llibres_Class13]
		[llibres_Class16]
		[recomanacio_Class20001])
	(preu 19.95)
	(titol "El tulipan negro"))

([llibres_Class13] of  Genere

	(nom_genere "Historia"))

([llibres_Class14] of  Genere

	(nom_genere "Negra"))

([llibres_Class15] of  Genere

	(nom_genere "Policiaca"))

([llibres_Class16] of  Genere

	(nom_genere "Politica"))

([llibres_Class17] of  Llibre

	(best-seller TRUE)
	(critica 9)
	(escrit_per [KB_109530_Class59])
	(format gran)
	(genere
		[llibres_Class15]
		[llibres_Class7]
		[KB_109530_Class64]
		[recomanacio_Class20001])
	(preu 13.95)
	(titol "La verdad sobre el caso mendoza"))

([llibres_Class18] of  Llibre

	(best-seller TRUE)
	(critica 9)
	(digital FALSE)
	(escrit_per [KB_109530_Class45])
	(format gran)
	(genere
		[KB_109530_Class41]
		[llibres_Class7]
		[KB_109530_Class64]
		[llibres_Class2]
		[recomanacio_Class20000])
	(preu 20.0)
	(titol "El hobbit"))

([llibres_Class2] of  Genere

	(genere_semblant [KB_109530_Class37])
	(nom_genere "Fantastic"))

([llibres_Class20] of  Llibre

	(best-seller TRUE)
	(critica 8)
	(escrit_per [KB_109530_Class60])
	(format de_butxaca)
	(genere
		[llibres_Class7]
		[llibres_Class13]
		[llibres_Class14]
		[recomanacio_Class20001])
	(preu 15.0)
	(titol "Cumbres borrascosas"))

([llibres_Class21] of  Llibre

	(critica 5)
	(digital FALSE)
	(escrit_per [KB_109530_Class49])
	(format gran)
	(genere
		[llibres_Class13]
		[llibres_Class16]
		[llibres_Class7]
		[recomanacio_Class20000])
	(preu 15.0)
	(titol "El general en su laberinto"))

([llibres_Class22] of  Llibre

	(critica 5)
	(escrit_per [KB_109530_Class49])
	(format de_butxaca)
	(genere
		[llibres_Class13]
		[llibres_Class7]
		[recomanacio_Class20001])
	(preu 12.5)
	(titol "Vivir par contarla"))

([llibres_Class23] of  Llibre

	(best-seller TRUE)
	(critica 7)
	(digital FALSE)
	(escrit_per [KB_109530_Class57])
	(format gran)
	(genere
		[llibres_Class7]
		[KB_109530_Class39]
		[recomanacio_Class20001])
	(preu 17.95)
	(titol "El resplandor"))

([llibres_Class24] of  Llibre

	(best-seller TRUE)
	(critica 8)
	(escrit_per [KB_109530_Class57])
	(format de_butxaca)
	(genere
		[llibres_Class7]
		[llibres_Class10]
		[KB_109530_Class39]
		[recomanacio_Class20001])
	(preu 12.95)
	(titol "El cazador de sueÃ±os"))

([llibres_Class25] of  Llibre

	(best-seller TRUE)
	(critica 6)
	(de_moda TRUE)
	(escrit_per [KB_109530_Class58])
	(format gran)
	(genere
		[KB_109530_Class64]
		[KB_109530_Class40]
		[llibres_Class9]
		[recomanacio_Class20001])
	(preu 25.5)
	(titol "Amanecer"))

([llibres_Class27] of  Llibre

	(best-seller TRUE)
	(critica 3)
	(de_moda TRUE)
	(escrit_per [KB_109530_Class58])
	(format gran)
	(genere
		[llibres_Class7]
		[KB_109530_Class64]
		[KB_109530_Class40]
		[llibres_Class9]
		[recomanacio_Class20001])
	(preu 23.5)
	(titol "Crepusculo"))

([llibres_Class28] of  Llibre

	(critica 7)
	(escrit_per [KB_109530_Class51])
	(format gran)
	(genere
		[llibres_Class13]
		[KB_109530_Class40]
		[llibres_Class7]
		[recomanacio_Class20000])
	(preu 15.95)
	(titol "Los miserables"))

([llibres_Class3] of  Genere

	(nom_genere "Oest"))

([llibres_Class4] of  Genere

	(genere_semblant [KB_109530_Class37])
	(nom_genere "Robots"))

([llibres_Class5] of  Genere

	(genere_semblant
		[KB_109530_Class37]
		[KB_109530_Class38]
		[llibres_Class4])
	(nom_genere "Cyber Punk"))

([llibres_Class6] of  Genere

	(genere_semblant
		[KB_109530_Class37]
		[KB_109530_Class42]
		[KB_109530_Class39])
	(nom_genere "Zombies"))

([llibres_Class7] of  Genere

	(genere_semblant [llibres_Class8])
	(nom_genere "Adult"))

([llibres_Class8] of  Genere

	(genere_semblant [llibres_Class7])
	(nom_genere "Erotic"))

([llibres_Class9] of  Genere

	(genere_semblant
		[KB_109530_Class42]
		[KB_109530_Class39]
		[llibres_Class6])
	(nom_genere "Vampirs"))

([recomanacio_Class0] of  Autor

	(genere_esc_preferit
		[KB_109530_Class63]
		[llibres_Class2]
		[recomanacio_Class10000])
	(ha_escrit
		[recomanacio_Class2]
		[recomanacio_Class1]
		[recomanacio_Class3]
		[recomanacio_Class5])
	(internacional TRUE)
	(nom "Hermanos Grimm")
	(pais_esc "AL"))

([recomanacio_Class1] of  Llibre

	(critica 8)
	(escrit_per [recomanacio_Class0])
	(format gran)
	(genere
		[llibres_Class2]
		[KB_109530_Class63]
		[recomanacio_Class10000])
	(preu 12.0)
	(titol "Hansel y Gretel"))

([recomanacio_Class10000] of  Genere

	(nom_genere "Conte classic"))

([recomanacio_Class10001] of  Genere

	(nom_genere "Conte actual"))

([recomanacio_Class10002] of  Genere

	(genere_semblant
		[recomanacio_Class10004]
		[recomanacio_Class10003])
	(nom_genere "Educatiu animals"))

([recomanacio_Class10003] of  Genere

	(nom_genere "Educatiu professions"))

([recomanacio_Class10004] of  Genere

	(nom_genere "Educatiu escolar"))

([recomanacio_Class10006] of  Genere

	(nom_genere "Interactiu"))

([recomanacio_Class10007] of  Genere

	(nom_genere "Novela"))

([recomanacio_Class10009] of  Autor

	(genere_esc_preferit
		[recomanacio_Class10002]
		[recomanacio_Class10004]
		[recomanacio_Class10003])
	(ha_escrit
		[recomanacio_Class10017]
		[recomanacio_Class10015]
		[recomanacio_Class10016]
		[recomanacio_Class10018]
		[recomanacio_Class10019])
	(nom "Manuel Infant")
	(pais_esc "ESP"))

([recomanacio_Class10010] of  Autor

	(genere_esc_preferit
		[recomanacio_Class10001]
		[KB_109530_Class63]
		[llibres_Class2])
	(ha_escrit
		[recomanacio_Class10014]
		[recomanacio_Class10012]
		[recomanacio_Class10013])
	(internacional TRUE)
	(nom "James Cool")
	(pais_esc "UK"))

([recomanacio_Class10012] of  Llibre

	(critica 6)
	(de_moda TRUE)
	(digital FALSE)
	(escrit_per [recomanacio_Class10010])
	(format gran)
	(genere
		[recomanacio_Class10001]
		[KB_109530_Class63])
	(preu 10.0)
	(punts 1.0)
	(titol "Pocoyo"))

([recomanacio_Class10013] of  Llibre

	(critica 8)
	(de_moda TRUE)
	(digital FALSE)
	(escrit_per [recomanacio_Class10010])
	(format gran)
	(genere
		[recomanacio_Class10001]
		[KB_109530_Class63])
	(preu 12.75)
	(punts 1.0)
	(titol "Pokemon"))

([recomanacio_Class10014] of  Llibre

	(critica 9)
	(de_moda TRUE)
	(digital FALSE)
	(escrit_per [recomanacio_Class10010])
	(genere
		[recomanacio_Class10001]
		[KB_109530_Class63])
	(preu 15.0)
	(punts 1.0)
	(titol "Ninja Turtles"))

([recomanacio_Class10015] of  Llibre

	(critica 7)
	(digital FALSE)
	(escrit_per [recomanacio_Class10009])
	(format gran)
	(genere
		[recomanacio_Class10004]
		[KB_109530_Class63])
	(preu 15.0)
	(punts 1.0)
	(titol "Apren a escriure"))

([recomanacio_Class10016] of  Llibre

	(critica 6)
	(digital FALSE)
	(escrit_per [recomanacio_Class10009])
	(format gran)
	(genere
		[recomanacio_Class10002]
		[KB_109530_Class63])
	(preu 8.5)
	(punts 1.0)
	(titol "La granja"))

([recomanacio_Class10017] of  Llibre

	(critica 5)
	(de_moda TRUE)
	(digital FALSE)
	(escrit_per [recomanacio_Class10009])
	(format de_butxaca)
	(genere
		[recomanacio_Class10002]
		[KB_109530_Class63])
	(preu 10.5)
	(punts 1.0)
	(titol "Animals del mar"))

([recomanacio_Class10018] of  Llibre

	(critica 3)
	(digital FALSE)
	(escrit_per [recomanacio_Class10009])
	(format de_butxaca)
	(genere
		[recomanacio_Class10003]
		[KB_109530_Class63])
	(preu 8.9)
	(punts 1.0)
	(titol "Les professions"))

([recomanacio_Class10019] of  Llibre

	(critica 9)
	(de_moda TRUE)
	(digital FALSE)
	(escrit_per [recomanacio_Class10009])
	(genere
		[recomanacio_Class10006]
		[KB_109530_Class64]
		[recomanacio_Class20001])
	(preu 20.0)
	(punts 1.0)
	(titol "La pena dels tigres interactiu"))

([recomanacio_Class2] of  Llibre

	(critica 6)
	(digital FALSE)
	(escrit_per [recomanacio_Class0])
	(format gran)
	(genere
		[llibres_Class2]
		[KB_109530_Class63]
		[recomanacio_Class10000])
	(preu 13.95)
	(titol "El gato con botas"))

([recomanacio_Class20000] of  Genere

	(nom_genere "Passat"))

([recomanacio_Class20001] of  Genere

	(nom_genere "Present"))

([recomanacio_Class20002] of  Genere

	(nom_genere "Futur"))

([recomanacio_Class3] of  Llibre

	(critica 7)
	(escrit_per [recomanacio_Class0])
	(format de_butxaca)
	(genere
		[llibres_Class2]
		[KB_109530_Class63]
		[recomanacio_Class10000])
	(preu 12.95)
	(titol "La oca de oro"))

([recomanacio_Class5] of  Llibre

	(critica 8)
	(escrit_per [recomanacio_Class0])
	(format de_butxaca)
	(genere
		[llibres_Class2]
		[KB_109530_Class63]
		[recomanacio_Class10000])
	(preu 10.95)
	(titol "Los musicos de Bremen"))

)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

					;;;;;;;;;;;;;;;;;;;;;;;;;;;
					;;;;    CODI CLIPS    ;;;;;
					;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++;;
;;++++++++++++++++	            Deftemplates                                    +++++++++++++++++++++;;
;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++;;

(deftemplate dades-lector
    (slot edat (allowed-strings "Infantil" "Juvenil" "Adult"))    
    (slot sexe (allowed-strings "h" "d"))
    (slot llocLectura (allowed-strings "casa" "fora"))
    (slot frequencia (allowed-strings "diariament" "ocasionalment"))
    (slot bestSellers (allowed-strings "TRUE" "FALSE"))
    (slot critiques (allowed-strings "TRUE" "FALSE"))
    (slot modes (allowed-strings "TRUE" "FALSE"))
	(multislot generes (type STRING))
    (multislot autors (type STRING))  
    (slot preu (type FLOAT))
)

(deftemplate puntuacions-llibres
	(multislot llibre (type INSTANCE) (allowed-classes Llibre))
    (multislot puntuacio (type FLOAT))
)

;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++;;
;;++++++++++++++++	            Deffunctions                                   +++++++++++++++++++++;;
;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++;;

(deffunction ask-integer (?question)
   (printout t ?question)
   (bind ?answer (read))
   (while (not (integerp ?answer)) do
      (printout t ?question)
      (bind ?answer (read))
   )
   (return ?answer)
)

(deffunction ask-float (?question)
   (printout t ?question)
   (bind ?answer (read))
   (while (not (floatp ?answer)) do
      (printout t ?question)
      (bind ?answer (read))
   )
   (return ?answer)
)

(deffunction ask-question (?question $?allowed-values)
   (printout t ?question)
   (bind ?answer (read))
   (if (lexemep ?answer) 
       then (bind ?answer (lowcase ?answer)))
   (while (not (member ?answer ?allowed-values)) do
      (printout t ?question)
      (bind ?answer (read))
      (if (lexemep ?answer) 
          then (bind ?answer (lowcase ?answer))))
   ?answer)

(deffunction ask-question-no-lowercase (?question $?allowed-values)
   (printout t ?question)
   (bind ?answer (read))
   (if (lexemep ?answer) 
       then (bind ?answer ?answer))
   (while (not (member ?answer ?allowed-values)) do
      (printout t ?question)
      (bind ?answer (read))
      (if (lexemep ?answer) 
          then (bind ?answer (lowcase ?answer))))
   ?answer)

(deffunction si-no(?question)
   (bind ?response (ask-question ?question si no))
   (switch ?response
		(case si then (return "TRUE"))
		(case no then  (return "FALSE"))
   )
)

(deffunction pregunta-multiple(?pregunta)
	(printout t ?pregunta)
	(bind ?resposta (read))
    (bind $?llista (create$))
	(while (not (eq (str-compare ?resposta "fet") 0))        
        (bind $?llista (insert$ ?llista 1 ?resposta))
		(printout t ?pregunta)
		(bind ?resposta (read))
	)
    (return ?llista)
)

(deffunction printAll()
	(do-for-all-instances ((?llibre Llibre)) TRUE
        ;(if (eq (str-compare ?llibre:de_moda TRUE) 0) then
            (printout t ?llibre:titol : ?llibre:punts crlf)
        ;)
    )
    (printout t crlf crlf /*******************************************************/ crlf crlf)    
)

;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++;;
;;++++++++++++++++	             Mï¿œdul MAIN                             			+++++++++++++++++++++;;
;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++;;

(defmodule MAIN (export ?ALL))

(defrule inicialitzar
	(declare (salience 10000))
	=>
	(printout t "************************************************************************" crlf)
	(printout t "************************************************************************" crlf)
  	(printout t "**|         Prï¿œctica de Sistemes Experts basats en el coneixement.   |**" crlf)
	(printout t "**|------------------------------------------------------------------|**" crlf)
	(printout t "**|               Curs 2011-2012. Quadrimestre de Primavera.         |**" crlf)
	(printout t "**|              Sergi Capo del Nido  -  Oriol Ganduxe Pregona.      |**" crlf)
	(printout t "**|__________________________________________________________________|**" crlf)
	(printout t "************************************************************************" crlf)
	(printout t "************************************************************************" crlf)
    (assert (dades-lector))
	(focus Preguntes)
)

;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++;;
;;++++++++++++++++	             Mï¿œdul de Preguntes                             +++++++++++++++++++++;;
;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++;;

(defmodule Preguntes (export ?ALL) (import MAIN ?ALL))

(defrule preguntar-edat
    (not (edat-ok))
	?lector <- (dades-lector)
	=>
	(bind ?edat (ask-integer "Quina edat tens?"))
    
    (if (< ?edat 13) then (bind ?edatAbs Infantil))
    (if (> ?edat 17) then (bind ?edatAbs Adult))
    (if (and(> ?edat 12)(< ?edat 18)) then (bind ?edatAbs Juvenil))
    
    (modify ?lector (edat ?edatAbs))
    (assert (edat-ok))
)

; No es fa servir
(defrule preguntar-sexe
    (not (sexe-ok))
	?lector <- (dades-lector)
	=>
	(bind ?sexe (ask-question "Ets un home o una dona? [h | d]" h d))    
    (modify ?lector (sexe ?sexe))
    (assert (sexe-ok))
)

(defrule preguntar-lloc
    (not (lloc-ok))
	?lector <- (dades-lector)
	=>
	(bind ?lloc (ask-question "On llegeixes normalment? [casa | fora]" casa fora))   
    (modify ?lector (llocLectura ?lloc))
    (assert (lloc-ok))
)

(defrule preguntar-frequencia
    (not (frequencia-ok))
	?lector <- (dades-lector)
	=>
	(bind ?freq (ask-question "Amb quina frequencia llegeixes normalment? [diariament | ocasionalment] " diariament ocasionalment))   
    (modify ?lector (frequencia ?freq))
    (assert (frequencia-ok))
)

(defrule preguntar-best-seller
    (not (best-seller-ok))
	?lector <- (dades-lector)
	=>
	(bind ?best (si-no "T'agraden els best sellers? [si | no] "))   
    (modify ?lector (bestSellers ?best))
    (assert (best-seller-ok))
)

(defrule preguntar-critiques
    (not (critiques-ok))
	?lector <- (dades-lector)
	=>
	(bind ?crit (si-no "Et refies de les critiques literaries? [si | no] ")) 
    (modify ?lector (critiques ?crit))
    (assert (critiques-ok))
)

(defrule preguntar-modes
    (not (modes-ok))
	?lector <- (dades-lector)
	=>
	(bind ?moda (si-no "Segueixes les modes literaries? [si | no] "))   
    (modify ?lector (modes ?moda))
    (assert (modes-ok))
)

(defrule preguntar-preu
    (not (preu-ok))
	?lector <- (dades-lector)
	=>
	(bind ?preu (ask-float "Quin preu t'agradaria pagar?"))  
    (if (< ?preu 15.0) then (bind ?preuAbs barat))
    (if (> ?preu 30.0) then (bind ?preuAbs car))
    (if (and(> ?preu 14.0)(< ?preu 31)) then (bind ?preuAbs normal))
     
    (modify ?lector (preu ?preuAbs))
    (assert (preu-ok))
)

(defrule saltar-heuristica
	(declare (salience -2))
    =>
    (focus PreguntesPersonalitzades)    
)
;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++;;
;;++++++++++++++++	             Modul de Preguntes Personalitzades                      +++++++++++++++++++++;;
;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++;;

;(load /home/serx/Escriptori/workspace/IA-RecomanacioLlibres/recomanacio/recomanacio_Sergi.clp)

(defmodule PreguntesPersonalitzades (export ?ALL) (import MAIN ?ALL))

(defrule es-petit
    (not (edat))
	?lector <- (dades-lector (edat Infantil))
	=>
    
    ; DESCARTAR
    (do-for-all-instances ((?llibre Llibre)) TRUE
 			;(printout t LLIBRE: ?llibre:titol ,NOMBRE GENERES: (length$ ?llibre:genere) crlf)
        	(bind ?aux 0)
	        (loop-for-count (?j 1 (length$ ?llibre:genere)) do
            
            	
            
	            (bind ?gen_dir (nth$ ?j ?llibre:genere)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	
            	;(printout t ?gen crlf crlf)
				;(bind ?gen "Infantil") 
            
            	(if (eq ( str-compare ?gen "Infantil" )0)  then
                	(bind ?aux 1)
                )
	        ) 
        	(if (eq ?aux 0) then
           		(send ?llibre delete)
            )
    )
    
    ;PREGUNTES
	(bind ?res (ask-question "Vols un llibre educatiu o un conte? [eduactiu | conte] " educatiu conte))   
    
    ; ASSERTS
    (assert (edat))
    (assert (tipusLlibre ?res))
)

(defrule es-conte-infantil
	(tipusLlibre conte)    
   
	=>
      
    ;PREGUNTES
	(bind ?res (ask-question "Vols un conte classic o actual? [classic | actual] " classic actual))   
    
    ; ASSERTS
    (assert (conte ?res))
)

;esborrem les instancies que no siguin de conte [classic | actual]
(defrule descartar-no-conte
	(conte ?tipus)    
   
	=>
    
    (switch ?tipus
    	(case classic then (bind ?tematica "Conte classic"))    
        (case actual then (bind ?tematica "Conte actual"))
    )
    
    ; DESCARTAR
    (do-for-all-instances ((?llibre Llibre)) TRUE
 
        	(bind ?aux 0)
	        (loop-for-count (?j 1 (length$ ?llibre:genere)) do
	            (bind ?gen_dir (nth$ ?j ?llibre:genere)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	
	 
            	(if (eq ( str-compare ?gen ?tematica)0)  then
                	(bind ?aux 1)
                )
	        ) 
        	(if (eq ?aux 0) then
           		(send ?llibre delete)
            ;(printout t ?llibre:titol crlf)
            )
    )
)

(defrule es-educatiu
	(tipusLlibre educatiu)    
   
	=>
       
    ;PREGUNTES
	(bind ?res (ask-question "Vols un llibre educatiu, de quin tema? [animals | professions |escolar]" animals professions escolar))   
    
    ; ASSERTS
    (assert (educatiu ?res))
)

;esborrem les instancies que no siguin educatiu [animals | professions | escolar]
(defrule descartar-no-educatiu
	(educatiu ?tema)    
   
	=>
    
    (switch ?tema
    	(case animals then (bind ?tematica "Educatiu animals"))    
        (case professions then (bind ?tematica "Educatiu professions"))
        (case escolar then (bind ?tematica "Educatiu escolar"))
    )
    
    ; DESCARTAR
    (do-for-all-instances ((?llibre Llibre)) TRUE
 
        	(bind ?aux 0)
	        (loop-for-count (?j 1 (length$ ?llibre:genere)) do
	            (bind ?gen_dir (nth$ ?j ?llibre:genere)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	
	 
            	(if (eq ( str-compare ?gen ?tematica)0)  then
                	(bind ?aux 1)
                )
	        ) 
        	(if (eq ?aux 0) then
           		(send ?llibre delete)
            ;(printout t ?llibre:titol crlf)
            )
    )
)



(defrule es-jove
    (not (edat))
	?lector <- (dades-lector (edat Juvenil))
	=>
    ; DESCARTAR
    (do-for-all-instances ((?llibre Llibre)) TRUE
        
        	(bind ?aux 0)
	        (loop-for-count (?j 1 (length$ ?llibre:genere)) do
	            (bind ?gen_dir (nth$ ?j ?llibre:genere)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	
	 
            	(if (eq ( str-compare ?gen "Juvenil" )0)  then
                	(bind ?aux 1)
                )
	        ) 
        	(if (eq ?aux 0) then
           		(send ?llibre delete)
            )
    )
    
    ;(load /home/serx/Escriptori/workspace/IA-RecomanacioLlibres/recomanacio/recomanacio_Sergi.clp)
    (do-for-all-instances ((?autor Autor)) TRUE
        ;(printour t " TOMAQUET   " crlf)
        	(bind ?aux 1)
	        (loop-for-count (?j 1 (length$ ?autor:genere_esc_preferit)) do
           ; (printour t " ENSALADA   " crlf)
	            (bind ?gen_dir (nth$ ?j ?autor:genere_esc_preferit)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	
	 
            	(if (eq ( str-compare ?gen "Infantil" )0)  then
                	(bind ?aux 0)
                )
	        ) 
        	(if (eq ?aux 0) then
                (printout t "BORRAT " ?autor:nom " per no ser adult" crlf)
           		(send ?autor delete)
            )
    )
    
    ;PREGUNTES
	(bind ?res (si-no "T'agraden els llibres interactius? [si | no] "))   
    (bind ?realFicc (ask-question "Prefereixes la realitat o la fantasia? [realitat | fantasia] " realitat fantasia)) 
    ; ASSERTS
    (assert (edat))
    (assert (interactiu ?res))
    (assert (real-ficcio ?realFicc))
)

;(load /home/serx/Escriptori/workspace/IA-RecomanacioLlibres/recomanacio/recomanacio.clp)
(defrule es-realitat
	(real-ficcio realitat)
    
	=>
    
    ;(bind ?genFiccio (create$ "Ciencia ficcio" "Cyber Punk" "Extraterrestres" "Fantastic" "Futurista" "Robots" "Vampirs" "Zombies"))
    ; DESCARTAR
    (do-for-all-instances ((?llibre Llibre)) TRUE
        
        	(bind ?aux 0)
	        (loop-for-count (?j 1 (length$ ?llibre:genere)) do
	            (bind ?gen_dir (nth$ ?j ?llibre:genere)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	
	 
            	(if (member$ ?gen (create$ "Historia" "Negra" "Psicologica"))  then
                	(bind ?aux 1)
                )
	        ) 
        	(if (eq ?aux 0) then
                (printout t "BORRAT " ?llibre:titol " per ser realista" crlf)
           		(send ?llibre delete)
            )
    )
 
    ;PREGUNTES
	(bind ?epoca (ask-question "En quina epoca prefereixes que se situi la historia? [passat | present | indif] " passat present indif))
	(bind ?hist (si-no "T'agrada la novela historica? [si | no] ")) 
	(bind ?neg (si-no "T'agrada la novela negra? [si | no] ")) 
  	(bind ?psico (si-no "T'agrada la novela psicologica? [si | no] ")) 
    ; ASSERTS
    (assert (epoca ?epoca))
    (assert (historica ?hist))
    (assert (negra ?neg))
    (assert (psicologica ?psico))  
)



(defrule es-fantasia
	(real-ficcio fantasia)
    
	=>
    
    
    
    ; DESCARTAR
    (do-for-all-instances ((?llibre Llibre)) TRUE
        
        	(bind ?aux 0)
	        (loop-for-count (?j 1 (length$ ?llibre:genere)) do
	            (bind ?gen_dir (nth$ ?j ?llibre:genere)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	
	 
            	(if (member$ ?gen (create$ "Ciencia ficcio" "Fantastic" "Terror"))  then
                	(bind ?aux 1)
                )
	        ) 
        	(if (eq ?aux 0) then
                (printout t "BORRAT " ?llibre:titol " per ser realista" crlf)
           		(send ?llibre delete)
            )
    )
 
    ;PREGUNTES
	(bind ?epoca (ask-question "En quina epoca prefereixes que se situi la historia? [passat | present |futur| indif] " passat present futur indif))   
	(bind ?cienc (si-no "T'agrada la ciencia-ficció? [si | no] ")) 
	(bind ?fant (si-no "T'agrada la fantasia clàssica? [si | no] ")) 
  	(bind ?terr (si-no "T'agrada la novela de terror? [si | no] ")) 
    ; ASSERTS
    (assert (epoca ?epoca))
    (assert (ciencia-ficcio ?fant))
    (assert (fantastic ?fant))
    (assert (terror ?terr))  
)

(defrule epoca-passat
    (epoca passat)
    =>
    ; DESCARTAR
    (do-for-all-instances ((?llibre Llibre)) TRUE
        
        	(bind ?aux 0)
	        (loop-for-count (?j 1 (length$ ?llibre:genere)) do
	            (bind ?gen_dir (nth$ ?j ?llibre:genere)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	
        	
            (if (eq ( str-compare ?gen "Passat" )0)  then
                	(bind ?aux 1)
                )
	        ) 
        	(if (eq ?aux 0) then
                (printout t "BORRAT " ?llibre:titol " per no ser del passat" crlf)
           		(send ?llibre delete)
            )
    )
)

(defrule epoca-present
    (epoca present)
    =>
    ; DESCARTAR
    (do-for-all-instances ((?llibre Llibre)) TRUE
        
        	(bind ?aux 0)
	        (loop-for-count (?j 1 (length$ ?llibre:genere)) do
	            (bind ?gen_dir (nth$ ?j ?llibre:genere)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	
        	
            (if (eq ( str-compare ?gen "Present" )0)  then
                	(bind ?aux 1)
                )
	        ) 
        	(if (eq ?aux 0) then
            	(printout t "BORRAT " ?llibre:titol " per no ser del present" crlf)
           		(send ?llibre delete)
            )
    )
)

(defrule epoca-futur
    (epoca futur)
    =>
    ; DESCARTAR
    (do-for-all-instances ((?llibre Llibre)) TRUE
        
        	(bind ?aux 0)
	        (loop-for-count (?j 1 (length$ ?llibre:genere)) do
	            (bind ?gen_dir (nth$ ?j ?llibre:genere)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	
        	
            (if (eq ( str-compare ?gen "Futur" )0)  then
                	(bind ?aux 1)
                )
	        ) 
        	(if (eq ?aux 0) then
            	(printout t "BORRAT " ?llibre:titol " per no ser del futur" crlf)
           		(send ?llibre delete)
            )
    )
)

(defrule historica-si
    (not(dades-historica-ok))
	(historica "TRUE")
    ?lector <- (dades-lector (autors $?auts))
	=>

    (bind $?llista_autors (create$))
    (do-for-all-instances((?autor Autor)) TRUE
	        (loop-for-count (?j 1 (length$ ?autor:genere_esc_preferit)) do
	            (bind ?gen_dir (nth$ ?j ?autor:genere_esc_preferit)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	

	            (if (eq ( str-compare ?gen "Historia" )0)  then
	                	 (bind ?llista_autors (insert$ ?llista_autors 1 ?autor))
                
		        )
            ) 
    )
    (bind $?ll_3_autors (create$))
    (bind ?i 1)
   	(while (and (< ?i 4) (not(eq(length$ ?llista_autors) 0)) ) do
        (bind ?rand (random 1 (length$ ?llista_autors)))
		(bind ?autor (nth$ ?rand ?llista_autors)) 
        
        (bind ?ll_3_autors (insert$ ?ll_3_autors 1 (send ?autor get-nom)))
        (bind $?llista_autors (delete-member$ ?llista_autors ?autor))
        (bind ?i (+ ?i 1))
   	)

    (bind ?text (str-cat "Quin autor de novela historica prefereixes? [" (nth$ 1 ?ll_3_autors) "(1) | "  (nth$ 2 ?ll_3_autors) "(2) | "  (nth$ 3 ?ll_3_autors) "(3) | indif] "))
    (bind ?aut (ask-question ?text 1 2 3 indif))   
	
    (bind ?Saut (str-cat ?aut))
    
    (if (not(eq(str-compare ?Saut indif)0)) then
        (bind $?llista (insert$ ?auts 1 (nth$ ?aut ?ll_3_autors))) 
        (modify ?lector (autors ?llista))  
    )
    
    ; DESCARTAR
 	; BUSCAR AUTORS I PREGUNTAR
    
    
    ;PREGUNTES

    ; ASSERTS
    (assert (dades-historica-ok))
)

(defrule historica-no
	(historica "FALSE")
	=>

    ; DESCARTAR
    (do-for-all-instances ((?llibre Llibre)) TRUE
        
        	(bind ?aux 0)
	        (loop-for-count (?j 1 (length$ ?llibre:genere)) do
	            (bind ?gen_dir (nth$ ?j ?llibre:genere)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	
	 
            	(if (eq ( str-compare ?gen "Historia" )0)  then
                	(bind ?aux 1)
                )
	        ) 
        	(if (eq ?aux 1) then
                (printout t "BORRAT " ?llibre:titol " per ser historica" crlf)
           		(send ?llibre delete)
            )
    )
    ;PREGUNTES 
    
    ; ASSERTS
)

(defrule negra-si
    (not(dades-negra-ok))
	(negra "TRUE")
    ?lector <- (dades-lector (autors $?auts))
	=>

    (bind $?llista_autors (create$))
    (do-for-all-instances((?autor Autor)) TRUE
	        (loop-for-count (?j 1 (length$ ?autor:genere_esc_preferit)) do
	            (bind ?gen_dir (nth$ ?j ?autor:genere_esc_preferit)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	

	            (if (eq ( str-compare ?gen "Negra" )0)  then
	                	 (bind ?llista_autors (insert$ ?llista_autors 1 ?autor))
                
		        )
            ) 
    )
    (bind $?ll_3_autors (create$))
    (bind ?i 1)
   	(while (and (< ?i 4) (not(eq(length$ ?llista_autors) 0)) ) do
        (bind ?rand (random 1 (length$ ?llista_autors)))
		(bind ?autor (nth$ ?rand ?llista_autors)) 
        
        (bind ?ll_3_autors (insert$ ?ll_3_autors 1 (send ?autor get-nom)))
        (bind $?llista_autors (delete-member$ ?llista_autors ?autor))
        (bind ?i (+ ?i 1))
   	)

    (bind ?text (str-cat "Quin autor de novela negra prefereixes? [" (nth$ 1 ?ll_3_autors) "(1) | "  (nth$ 2 ?ll_3_autors) "(2) | "  (nth$ 3 ?ll_3_autors) "(3) | indif] "))
    (bind ?aut (ask-question ?text 1 2 3 indif))   
	
    (bind ?Saut (str-cat ?aut))
    
    (if (not(eq(str-compare ?Saut indif)0)) then
        (bind $?llista (insert$ ?auts 1 (nth$ ?aut ?ll_3_autors))) 
        (modify ?lector (autors ?llista))  
    )
    
    ; DESCARTAR
 	; BUSCAR AUTORS I PREGUNTAR
    
    
    ;PREGUNTES

    ; ASSERTS
    (assert (dades-negra-ok))
)

(defrule negra-no
	(negra "FALSE")
	=>

    ; DESCARTAR
    (do-for-all-instances ((?llibre Llibre)) TRUE
        
        	(bind ?aux 0)
	        (loop-for-count (?j 1 (length$ ?llibre:genere)) do
	            (bind ?gen_dir (nth$ ?j ?llibre:genere)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	
	 
            	(if (eq ( str-compare ?gen "Negra" )0)  then
                	(bind ?aux 1)
                )
	        ) 
        	(if (eq ?aux 1) then
                (printout t "BORRAT " ?llibre:titol " per ser negra" crlf)
           		(send ?llibre delete)
            )
    )
    ;PREGUNTES 
    
    ; ASSERTS
)

(defrule psicologica-si
    (not(dades-psicologica-ok))
	(psicologica "TRUE")
    ?lector <- (dades-lector (autors $?auts))
	=>

    (bind $?llista_autors (create$))
    (do-for-all-instances((?autor Autor)) TRUE
	        (loop-for-count (?j 1 (length$ ?autor:genere_esc_preferit)) do
	            (bind ?gen_dir (nth$ ?j ?autor:genere_esc_preferit)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	

	            (if (eq ( str-compare ?gen "Psicologica" )0)  then
	                	 (bind ?llista_autors (insert$ ?llista_autors 1 ?autor))
                
		        )
            ) 
    )
    (bind $?ll_3_autors (create$))
    (bind ?i 1)
   	(while (and (< ?i 4) (not(eq(length$ ?llista_autors) 0)) ) do
        (bind ?rand (random 1 (length$ ?llista_autors)))
		(bind ?autor (nth$ ?rand ?llista_autors)) 
        
        (bind ?ll_3_autors (insert$ ?ll_3_autors 1 (send ?autor get-nom)))
        (bind $?llista_autors (delete-member$ ?llista_autors ?autor))
        (bind ?i (+ ?i 1))
   	)

    (bind ?text (str-cat "Quin autor de novela psicologica prefereixes? [" (nth$ 1 ?ll_3_autors) "(1) | "  (nth$ 2 ?ll_3_autors) "(2) | "  (nth$ 3 ?ll_3_autors) "(3) | indif] "))
    (bind ?aut (ask-question ?text 1 2 3 indif))   
	
    (bind ?Saut (str-cat ?aut))
    
    (if (not(eq(str-compare ?Saut indif)0)) then
        (bind $?llista (insert$ ?auts 1 (nth$ ?aut ?ll_3_autors))) 
        (modify ?lector (autors ?llista))  
    )
    
    ; DESCARTAR
 	; BUSCAR AUTORS I PREGUNTAR
    
    
    ;PREGUNTES

    ; ASSERTS
    (assert (dades-psicologica-ok))
)

(defrule psicologica-no
	(psicologica "FALSE")
	=>

    ; DESCARTAR
    (do-for-all-instances ((?llibre Llibre)) TRUE
        
        	(bind ?aux 0)
	        (loop-for-count (?j 1 (length$ ?llibre:genere)) do
	            (bind ?gen_dir (nth$ ?j ?llibre:genere)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	
	 
            	(if (eq ( str-compare ?gen "Psicologica" )0)  then
                	(bind ?aux 1)
                )
	        ) 
        	(if (eq ?aux 1) then
                (printout t "BORRAT " ?llibre:titol " per ser psicologica" crlf)
           		(send ?llibre delete)
            )
    )
    ;PREGUNTES 
    
    ; ASSERTS
)

(defrule ciencia-ficcio-si
    (not(dades-ciencia-ficcio-ok))
	(ciencia-ficcio "TRUE")
    ?lector <- (dades-lector (autors $?auts))
	=>

    (bind $?llista_autors (create$))
    (do-for-all-instances((?autor Autor)) TRUE
	        (loop-for-count (?j 1 (length$ ?autor:genere_esc_preferit)) do
	            (bind ?gen_dir (nth$ ?j ?autor:genere_esc_preferit)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	

	            (if (eq ( str-compare ?gen "Ciencia Ficcio" )0)  then
	                	 (bind ?llista_autors (insert$ ?llista_autors 1 ?autor))
                
		        )
            ) 
    )
    (bind $?ll_3_autors (create$))
    (bind ?i 1)
   	(while (and (< ?i 4) (not(eq(length$ ?llista_autors) 0)) ) do
        (bind ?rand (random 1 (length$ ?llista_autors)))
		(bind ?autor (nth$ ?rand ?llista_autors)) 
        
        (bind ?ll_3_autors (insert$ ?ll_3_autors 1 (send ?autor get-nom)))
        (bind $?llista_autors (delete-member$ ?llista_autors ?autor))
        (bind ?i (+ ?i 1))
   	)

    (bind ?text (str-cat "Quin autor de ciencia ficció prefereixes? [" (nth$ 1 ?ll_3_autors) "(1) | "  (nth$ 2 ?ll_3_autors) "(2) | "  (nth$ 3 ?ll_3_autors) "(3) | indif] "))
    (bind ?aut (ask-question ?text 1 2 3 indif))   
	
    (bind ?Saut (str-cat ?aut))
    
    (if (not(eq(str-compare ?Saut indif)0)) then
        (bind $?llista (insert$ ?auts 1 (nth$ ?aut ?ll_3_autors))) 
        (modify ?lector (autors ?llista))  
    )
    
    ; DESCARTAR
 	; BUSCAR AUTORS I PREGUNTAR
    
    
    ;PREGUNTES

    ; ASSERTS
    (assert (dades-ciencia-ficcio-ok))
)

(defrule ciencia-ficcio-no
	(ciencia-ficcio "FALSE")
	=>

    ; DESCARTAR
    (do-for-all-instances ((?llibre Llibre)) TRUE
        
        	(bind ?aux 0)
	        (loop-for-count (?j 1 (length$ ?llibre:genere)) do
	            (bind ?gen_dir (nth$ ?j ?llibre:genere)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	
	 
            	(if (eq ( str-compare ?gen "Ciencia Ficcio" )0)  then
                	(bind ?aux 1)
                )
	        ) 
        	(if (eq ?aux 1) then
                (printout t "BORRAT " ?llibre:titol " per ser ciencia ficció" crlf)
           		(send ?llibre delete)
            )
    )
    ;PREGUNTES 
    
    ; ASSERTS
)

(defrule fantastic-si
    (not(dades-fantastic-ok))
	(fantastic "TRUE")
    ?lector <- (dades-lector (autors $?auts))
	=>

    (bind $?llista_autors (create$))
    (do-for-all-instances((?autor Autor)) TRUE
	        (loop-for-count (?j 1 (length$ ?autor:genere_esc_preferit)) do
	            (bind ?gen_dir (nth$ ?j ?autor:genere_esc_preferit)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	

	            (if (eq ( str-compare ?gen "Fantastic" )0)  then
	                	 (bind ?llista_autors (insert$ ?llista_autors 1 ?autor))
                
		        )
            ) 
    )
    (bind $?ll_3_autors (create$))
    (bind ?i 1)
   	(while (and (< ?i 4) (not(eq(length$ ?llista_autors) 0)) ) do
        (bind ?rand (random 1 (length$ ?llista_autors)))
		(bind ?autor (nth$ ?rand ?llista_autors)) 
        
        (bind ?ll_3_autors (insert$ ?ll_3_autors 1 (send ?autor get-nom)))
        (bind $?llista_autors (delete-member$ ?llista_autors ?autor))
        (bind ?i (+ ?i 1))
   	)

    (bind ?text (str-cat "Quin autor de novela de fantasia clàssica prefereixes? [" (nth$ 1 ?ll_3_autors) "(1) | "  (nth$ 2 ?ll_3_autors) "(2) | "  (nth$ 3 ?ll_3_autors) "(3) | indif] "))
    (bind ?aut (ask-question ?text 1 2 3 indif))   
	
    (bind ?Saut (str-cat ?aut))
    
    (if (not(eq(str-compare ?Saut indif)0)) then
        (bind $?llista (insert$ ?auts 1 (nth$ ?aut ?ll_3_autors))) 
        (modify ?lector (autors ?llista))  
    )
    
    ; DESCARTAR
 	; BUSCAR AUTORS I PREGUNTAR
    
    
    ;PREGUNTES

    ; ASSERTS
    (assert (dades-fantastic-ok))
)

(defrule fantastic-no
	(fantastic "FALSE")
	=>

    ; DESCARTAR
    (do-for-all-instances ((?llibre Llibre)) TRUE
        
        	(bind ?aux 0)
	        (loop-for-count (?j 1 (length$ ?llibre:genere)) do
	            (bind ?gen_dir (nth$ ?j ?llibre:genere)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	
	 
            	(if (eq ( str-compare ?gen "Fantastic" )0)  then
                	(bind ?aux 1)
                )
	        ) 
        	(if (eq ?aux 1) then
                (printout t "BORRAT " ?llibre:titol " per ser fantastic" crlf)
           		(send ?llibre delete)
            )
    )
    ;PREGUNTES 
    
    ; ASSERTS
)

(defrule terror-si
    (not(dades-terror-ok))
	(terror "TRUE")
    ?lector <- (dades-lector (autors $?auts))
	=>

    (bind $?llista_autors (create$))
    (do-for-all-instances((?autor Autor)) TRUE
	        (loop-for-count (?j 1 (length$ ?autor:genere_esc_preferit)) do
	            (bind ?gen_dir (nth$ ?j ?autor:genere_esc_preferit)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	

	            (if (eq ( str-compare ?gen "Terror" )0)  then
	                	 (bind ?llista_autors (insert$ ?llista_autors 1 ?autor))
                
		        )
            ) 
    )
    (bind $?ll_3_autors (create$))
    (bind ?i 1)
   	(while (and (< ?i 4) (not(eq(length$ ?llista_autors) 0)) ) do
        (bind ?rand (random 1 (length$ ?llista_autors)))
		(bind ?autor (nth$ ?rand ?llista_autors)) 
        
        (bind ?ll_3_autors (insert$ ?ll_3_autors 1 (send ?autor get-nom)))
        (bind $?llista_autors (delete-member$ ?llista_autors ?autor))
        (bind ?i (+ ?i 1))
   	)

    (bind ?text (str-cat "Quin autor de novela de terror prefereixes? [" (nth$ 1 ?ll_3_autors) "(1) | "  (nth$ 2 ?ll_3_autors) "(2) | "  (nth$ 3 ?ll_3_autors) "(3) | indif] "))
    (bind ?aut (ask-question ?text 1 2 3 indif))   
	
    (bind ?Saut (str-cat ?aut))
    
    (if (not(eq(str-compare ?Saut indif)0)) then
        (bind $?llista (insert$ ?auts 1 (nth$ ?aut ?ll_3_autors))) 
        (modify ?lector (autors ?llista))  
    )
    
    ; DESCARTAR
 	; BUSCAR AUTORS I PREGUNTAR
    
    
    ;PREGUNTES

    ; ASSERTS
    (assert (dades-terror-ok))
)

(defrule terror-no
	(terror "FALSE")
	=>

    ; DESCARTAR
    (do-for-all-instances ((?llibre Llibre)) TRUE
        
        	(bind ?aux 0)
	        (loop-for-count (?j 1 (length$ ?llibre:genere)) do
	            (bind ?gen_dir (nth$ ?j ?llibre:genere)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	
	 
            	(if (eq ( str-compare ?gen "Terror" )0)  then
                	(bind ?aux 1)
                )
	        ) 
        	(if (eq ?aux 1) then
                (printout t "BORRAT " ?llibre:titol " per ser terror" crlf)
           		(send ?llibre delete)
            )
    )
    ;PREGUNTES 
    
    ; ASSERTS
)

;esborrem les instancies que no siguin interactius
(defrule descartar-no-interactiu
	(interactiu "TRUE")    
   
	=>
    
    ; DESCARTAR
    (do-for-all-instances ((?llibre Llibre)) TRUE
 
        	(bind ?aux 0)
	        (loop-for-count (?j 1 (length$ ?llibre:genere)) do
	            (bind ?gen_dir (nth$ ?j ?llibre:genere)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	
	 
            	(if (eq ( str-compare ?gen "Interactiu")0)  then
                	(bind ?aux 1)
                )
	        ) 
        	(if (eq ?aux 0) then
           		(send ?llibre delete)
            ;(printout t ?llibre:titol crlf)
            )
    )
)

;esborrem les instancies que siguin interactius
(defrule descartar-interactiu
	(interactiu "FALSE")    
   
	=>
    
    ; DESCARTAR
    (do-for-all-instances ((?llibre Llibre)) TRUE
 
        	(bind ?aux 1)
	        (loop-for-count (?j 1 (length$ ?llibre:genere)) do
	            (bind ?gen_dir (nth$ ?j ?llibre:genere)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	
	 
            	(if (eq ( str-compare ?gen Interactiu)0)  then
                	(bind ?aux 0)
                )
	        ) 
        	(if (eq ?aux 0) then
           		(send ?llibre delete)
            ;(printout t ?llibre:titol crlf)
            )
    )
)

(defrule es-adult
    (not (edat))
	?lector <- (dades-lector (edat Adult))
	=>
    ; DESCARTAR
    (do-for-all-instances ((?llibre Llibre)) TRUE
        
        	(bind ?aux 0)
	        (loop-for-count (?j 1 (length$ ?llibre:genere)) do
	            (bind ?gen_dir (nth$ ?j ?llibre:genere)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	
	 
            	(if (eq ( str-compare ?gen "Adult" )0)  then
                	(bind ?aux 1)
                )
	        ) 
        	(if (eq ?aux 0) then
                (printout t "BORRAT " ?llibre:titol " per no ser adult" crlf)
           		(send ?llibre delete)
            )
    )
    
    ;(load /home/serx/Escriptori/workspace/IA-RecomanacioLlibres/recomanacio/recomanacio_Sergi.clp)
    (do-for-all-instances ((?autor Autor)) TRUE
        ;(printour t " TOMAQUET   " crlf)
        	(bind ?aux 1)
	        (loop-for-count (?j 1 (length$ ?autor:genere_esc_preferit)) do
           ; (printour t " ENSALADA   " crlf)
	            (bind ?gen_dir (nth$ ?j ?autor:genere_esc_preferit)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	
	 
            	(if (eq ( str-compare ?gen "Infantil" )0)  then
                	(bind ?aux 0)
                )
	        ) 
        	(if (eq ?aux 0) then
                (printout t "BORRAT " ?autor:nom " per no ser adult" crlf)
           		(send ?autor delete)
            )
    )
    
    ;PREGUNTES
	(bind ?dur (si-no "Toleres el contingut dur? [si | no] "))
    (bind ?realFicc (ask-question "Prefereixes la realitat o la fantasia? [realitat | fantasia] " realitat fantasia)) 
    
    ; ASSERTS
    (assert (contingut-dur ?dur))
    (assert (real-ficcio ?realFicc))
    (assert (edat))
    
)

(defrule contingut-dur
	(contingut-dur "TRUE")
	=>
    ; DESCARTAR
    
    ;PREGUNTES
	(bind ?gore (si-no "T'agrada el gore? [si | no] "))
	(bind ?erotic (si-no "T'agrada el contingut eròtic? [si | no] ")) 
    
    ; ASSERTS
    (assert (gore ?gore))
    (assert (erotic ?erotic))  
)

(defrule no-contingut-dur
	(contingut-dur "FALSE")
	=>
    ; DESCARTAR
    (do-for-all-instances ((?llibre Llibre)) TRUE
        
        	(bind ?aux 0)
	        (loop-for-count (?j 1 (length$ ?llibre:genere)) do
	            (bind ?gen_dir (nth$ ?j ?llibre:genere)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	
	 
            	(if (or (eq ( str-compare ?gen "Gore" )0) (eq ( str-compare ?gen "Erotic" )0))  then
                	(printout t "BORRAT " ?llibre:titol " per contingut dur" crlf)
                	(send ?llibre delete)
                )
	        ) 
    )
    
    ;PREGUNTES
    
    ; ASSERTS 
)

(defrule no-gore
	(gore "FALSE")
	=>
    ; DESCARTAR
    (do-for-all-instances ((?llibre Llibre)) TRUE
        
        	(bind ?aux 0)
	        (loop-for-count (?j 1 (length$ ?llibre:genere)) do
	            (bind ?gen_dir (nth$ ?j ?llibre:genere)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	
	 
            	(if (eq ( str-compare ?gen "Gore" )0)  then
                    (printout t "BORRAT " ?llibre:titol " per ser gore" crlf)
                	(send ?llibre delete)
                )
	        ) 
    )
    
    ;PREGUNTES
    
    ; ASSERTS 
)

(defrule no-erotic
	(erotic "FALSE")
	=>
    ; DESCARTAR
    (do-for-all-instances ((?llibre Llibre)) TRUE
        
        	(bind ?aux 0)
	        (loop-for-count (?j 1 (length$ ?llibre:genere)) do
	            (bind ?gen_dir (nth$ ?j ?llibre:genere)) 
	           	(bind ?gen (send (instance-address * ?gen_dir) get-nom_genere))  	
	 
            	(if (eq ( str-compare ?gen "Erotic" )0)  then
                    (printout t "BORRAT " ?llibre:titol " per ser erotic" crlf)
                (send ?llibre delete)
                )
	        ) 
    )
    
    ;PREGUNTES
    
    ; ASSERTS 
)

(defrule bestSellers-si
	?lector <- (dades-lector (bestSellers "TRUE"))
	=>
    ; DESCARTAR
    
    ;PREGUNTES
	(bind ?best (si-no "T'agrada el best-seller americà? [si | no] "))
    
    ; ASSERTS 
    (assert(bestAmerica ?best))
)

(defrule bestSellers-no
	?lector <- (dades-lector (bestSellers "FALSE"))
	=>
    ; DESCARTAR
    (do-for-all-instances ((?llibre Llibre)) TRUE

            (if (eq ( str-compare ?llibre:best-seller "TRUE" )0)  then
                    (printout t "BORRAT " ?llibre:titol " per ser bestSeller" crlf)
                	(send ?llibre delete)
 			)
    )
    ;PREGUNTES
	(bind ?best (si-no "T'agrada la novela extrangera? [si | no] "))
    
    ; ASSERTS 
    (assert(novela-extrangera ?best))
)


(defrule bestSeller-America-No
	(bestAmerica "FALSE")
    =>
    ; DESCARTAR
    (do-for-all-instances ((?llibre Llibre)) TRUE
        	(bind ?nacionalitat (send (instance-address * ?llibre:escrit_per) get-pais_esc))

            (if (and (eq ( str-compare ?nacionalitat "USA" )0) (eq ( str-compare ?llibre:best-seller "TRUE" )0)) then
                (printout t "BORRAT " ?llibre:titol " per ser bestSeller America" crlf)
                (send ?llibre delete)
            )

    )
    ;PREGUNTES
    
    ; ASSERTS 
)

(defrule novela-extrangera-No
	(novela-extrangera "FALSE")
    =>
    ; DESCARTAR
    (do-for-all-instances ((?llibre Llibre)) TRUE
        	(bind ?nacionalitat (send (instance-address * ?llibre:escrit_per) get-pais_esc))

            (if (not (eq ( str-compare ?nacionalitat "ESP" )0))  then
                (printout t "BORRAT " ?llibre:titol " per no ser novela extrangera" crlf)
                (send ?llibre delete)
            )

    )
    ;PREGUNTES
    
    ; ASSERTS 
)

(defrule moda-No
	?lector <- (dades-lector (modes "FALSE"))
	=>
    ; DESCARTAR
    (do-for-all-instances ((?llibre Llibre)) TRUE

            (if (eq ( str-compare ?llibre:de_moda "TRUE" )0)  then
                    (printout t "BORRAT " ?llibre:titol " per estar de moda" crlf)
                	(send ?llibre delete)
 			)
    )
    
    ;PREGUNTES
    
    ; ASSERTS 
)

(defrule fora-casa
    (not (format))
	?lector <- (dades-lector (llocLectura fora))
	=>
    ; DESCARTAR
    (do-for-all-instances ((?llibre Llibre)) TRUE

            (if (eq (str-compare ?llibre:format "gran" )0)  then
                    (printout t "BORRAT " ?llibre:titol " per ser massa gran" crlf)
                	(send ?llibre delete)
 			)
    )
    
    ;PREGUNTES
    (bind ?form (ask-question "Quin format prefereixes? [butxaca | digital | indif] " butxaca digital indif)) 
    
    ; ASSERTS
    (assert (format ?form))
)

(defrule fromat-butxaca
	(format butxaca)
    =>
    (do-for-all-instances ((?llibre Llibre)) TRUE

            (if (not(eq (str-compare ?llibre:format "de_butxaca" )0))  then
                    (printout t "BORRAT " ?llibre:titol " per no ser de butxaca" crlf)
                	(send ?llibre delete)
 			)
    )   
)

(defrule fromat-digital
	(format digital)
    =>
    (do-for-all-instances ((?llibre Llibre)) TRUE

            (if (not(eq (str-compare ?llibre:format "digital" )0))  then
                    (printout t "BORRAT " ?llibre:titol " per no ser digital" crlf)
                	(send ?llibre delete)
 			)
    )   
)

(defrule frequencia
    (not (freq))
	?lector <- (dades-lector (frequencia ocasionalment))
	=>
    ; TODO -> Descartar llibres llargs (caracteristica)
    ; DESCARTAR
    ;(do-for-all-instances ((?llibre Llibre)) TRUE
;
 ;           (if (eq (str-compare ?llibre:format "gran" )0)  then
  ;                  (printout t "BORRAT " ?llibre:titol " per ser massa gran" crlf)
   ;             	(send ?llibre delete)
 	;		)
	;)
    
    ;PREGUNTES
    (bind ?fr (ask-question "Quan sols llegit? [puntualment | vacances] " puntualment vacances)) 
    
    ; ASSERTS
    (assert (freq ?fr))
)

(defrule freq-puntual
	(freq puntualment)
    =>
    ; TODO -> Descartar llibres complicats (caracteristica)
    ;(do-for-all-instances ((?llibre Llibre)) TRUE
;
  ;          (if (not(eq (str-compare ?llibre:format "digital" )0))  then
   ;                 (printout t "BORRAT " ?llibre:titol " per no ser digital" crlf)
   ;             	(send ?llibre delete)
 	;		)
   ;)   
)

(defrule saltar-seleccio
	(declare (salience -2))
    =>
    (printAll)
    (printout t "saltem a heuristica " crlf)
    (focus Heuristica)    
)



;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++;;
;;++++++++++++++++	             Mï¿œdul de heurï¿œstica                            +++++++++++++++++++++;;
;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++;;

(defmodule Heuristica (export ?ALL) (import MAIN ?ALL))

(defrule puntuar-llibres-autor
    ?llibre <- (object (is-a Llibre) (titol ?titol) (escrit_per ?autor));
    ?preferencies <- (dades-lector (autors $?autors))
    
    =>
    
    (bind ?nomAutor (send (instance-address * ?autor) get-nom))   
    (bind ?puntuacio (send ?llibre get-punts))
    (loop-for-count (?i 1 (length$ ?autors)) do    
        (bind ?aux (nth$ ?i ?autors))
		(if (eq (str-compare ?nomAutor ?aux)0)  then
            (bind ?puntuacio (* ?puntuacio 1.3))
        )
	)
    ;(printout t ?titol ": " ?puntuacio crlf)     
)

(defrule puntuar-llibres-moda
    ?llibre <- (object (is-a Llibre) (titol ?titol) (de_moda ?moda));
    ?preferencies <- (dades-lector (modes "TRUE")) 
    
    =>    
    
    (bind ?puntuacio (send ?llibre get-punts))
    (if (eq TRUE ?moda)  then
            (bind ?puntuacio (* ?puntuacio 1.2))
    )
    ;(printout t ?titol ": " ?puntuacio crlf)  
    (send ?llibre put-punts ?puntuacio)
)  

(defrule puntuar-llibres-best-seller
    ?llibre <- (object (is-a Llibre) (titol ?titol) (best-seller ?best));
    ?preferencies <- (dades-lector (bestSellers "TRUE")) 
    
    =>    
    
    (bind ?puntuacio (send ?llibre get-punts))
    (if (eq TRUE ?best)  then
            (bind ?puntuacio (* ?puntuacio 1.2))
    )
    ;(printout t ?titol ": " ?puntuacio crlf)  
    (send ?llibre put-punts ?puntuacio)
)  

(defrule puntuar-llibres-cririca
    ?llibre <- (object (is-a Llibre) (titol ?titol) (critica ?critica));
    ?preferencies <- (dades-lector (critiques "TRUE")) 
    
    =>    
    
    (bind ?puntuacio (send ?llibre get-punts))
    (bind ?puntuacio (* ?puntuacio ?critica))  
    (send ?llibre put-punts ?puntuacio)
)  

(defrule puntuar-llibres-preu
   ?llibre <- (object (is-a Llibre) (titol ?titol) (preu ?preuLlibre));
   ?preferencies <- (dades-lector (preu ?preuLector))
    
   =>
    
   (if (< ?preuLlibre 15.0) then (bind ?preuAbs barat))
   (if (> ?preuLlibre 30.0) then (bind ?preuAbs car))
   (if (and(> ?preuLlibre 14.0)(< ?preuLlibre 31)) then (bind ?preuAbs normal))
    
   (bind ?puntuacio (send ?llibre get-punts))
   (if (not(eq ?preuAbs ?preuLector)) then
        
        (switch ?preuLector
			(case barat then 
                (if (eq ?preuAbs normal) then
              		(printout t ?preuAbs crlf)
                 else
                    (printout t ?preuAbs crlf)
                )
            )
            (case normal then 
                (if (eq ?preuAbs car) then
              		(printout t ?preuAbs crlf)
            	) 
            )       
        )
   )
)

(defrule imprimir-instancies
    (declare (salience -1))
    
    =>
    
    (do-for-all-instances ((?llibre Llibre)) TRUE
        ;(if (eq (str-compare ?llibre:de_moda TRUE) 0) then
            (printout t ?llibre:titol : ?llibre:punts crlf)
        ;)
    )
    (printout t crlf crlf /*******************************************************/ crlf crlf)
)
    
   ; (if (> ?preuLlibre ?preuLector)  then
   ;     	(bind ?factor (/ (- ?preuLlibre ?preuLector) 20))
   ;         (bind ?puntuacio (* ?puntuacio (- 1 ?factor)))
   ; )
    ;(printout t ?titol ": " ?puntuacio crlf)  
;) 

(defrule saltar-seleccio
	(declare (salience -2))
    =>
    (focus Seleccio)    
)


;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++;;
;;++++++++++++++++	             Mï¿œdul de selecciï¿œ                             +++++++++++++++++++++;;
;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++;;

(defmodule Seleccio (export ?ALL) (import MAIN ?ALL))

(defrule seleccionar-millors-llibres
    (not (fi-seleccio))
    
    =>
    
	(loop-for-count (?i 1 3) do  
    	(bind ?puntsMax 0)
    	(bind ?titolMax "")
        
    	(do-for-all-instances ((?llibre Llibre)) TRUE
        	(if (> ?llibre:punts ?puntsMax) then
				(bind ?puntsMax ?llibre:punts)    
            	(bind ?titolMax ?llibre:titol)        
            )
        )
        
        (do-for-all-instances ((?llibre Llibre)) TRUE
        	(if (eq (str-compare ?llibre:titol ?titolMax) 0) then
				(send ?llibre delete)       
            )
        )
        (printout t OPCIO ?i : ?titolMax " (amb " ?puntsMax " punts)" crlf)
	)
    (assert (fi-seleccio))
)	
        


